<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:x="http://schemas.microsoft.com/xps/2005/06/resourcedictionary-key">

  <xsl:output method="xml" version="1.0" omit-xml-declaration="yes" indent="yes" encoding="utf-8"/>

  <xsl:variable name="UnitCount" select="count(/ProjectDataSet/Project/BacklogItemUnit[Type != 6])" />
  <xsl:variable name="SprintCount" select="count(/ProjectDataSet/Project/Sprint)" />



  <xsl:include href="include/Slices.xslt"/>

  <xsl:variable name="plannedSprints" select="/ProjectDataSet/Project/Sprint[SprintNumber = /ProjectDataSet/Project/Baseline]"/>

  <xsl:template match="/">

    <FlowDocument
      PageWidth="21cm"
      PagePadding="60,40,40,40"
      PageHeight="29.7cm"
      ColumnWidth="21 cm">

      <xsl:call-template name="StyleSettings"/>

      <xsl:call-template name="SprintHeader">
        <xsl:with-param name="type" select="' - Selected Backlog'"/>
        <xsl:with-param name="sprintNumber" select="/ProjectDataSet/Project/Baseline"/>
      </xsl:call-template>

      <xsl:call-template name ="BaseLineCheck"/>

      <xsl:call-template name ="ProjectOverCheck"/>

      <xsl:if test="$currentSprintNumber">
        <Table Style="{{StaticResource NoBorderTable}}">

          <Table.Columns>
            <TableColumn Width="40"/>
            <TableColumn Width="300"/>
            <TableColumn Width="40"/>
            <TableColumn Width="40"/>
            <TableColumn Width="40"/>
          </Table.Columns>

          <TableRowGroup>
            <!-- SPRINT TOP -->
            <TableRow>
              <TableCell ColumnSpan="5" BorderThickness="0,0,0,0.5"/>
              <xsl:for-each select="$plannedSprints">
                <xsl:sort select="SprintNumber" data-type="number" order="descending" />
                <xsl:sort select="SprintUId" />


                <xsl:variable name="baseline" select="SprintNumber"/>

                <xsl:variable name="plannedUnits"
                              select="../BacklogItemUnit[UnitName = key('Hours_By_BaseLine',$baseline)[Quantity &gt; 0]/UnitName]"/>


                <xsl:variable name="validColspan">
                  <xsl:choose>
                    <xsl:when test="count($plannedUnits) &gt; 0">
                      <xsl:value-of select="count($plannedUnits)"/>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="1"/>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:variable>                
                <TableCell Style="{{StaticResource RightBorderCellOnly}}" ColumnSpan="{$validColspan}">
                  <Paragraph TextAlignment="Right">
                    <Bold>
                      <xsl:value-of select="SprintNumber"/>&#186; Plan
                    </Bold>
                  </Paragraph>
                </TableCell>
              </xsl:for-each>
            </TableRow>

            <!-- BACKLOGITEM HEADER -->
            <TableRow Style="{{StaticResource HeaderRow}}">
              <TableCell ColumnSpan="3">
                <Paragraph>Item</Paragraph>
              </TableCell>
              <TableCell>
                <Paragraph>B.V.</Paragraph>
              </TableCell>
              <TableCell>
                <Paragraph>Size.</Paragraph>
              </TableCell>
              <xsl:for-each select="$plannedSprints" >
                <xsl:sort select ="SprintNumber" data-type="number" order="descending"/>

                <xsl:variable name="baseline" select="SprintNumber"/>

                <xsl:variable name="plannedUnits"
                              select="../BacklogItemUnit[UnitName = key('Hours_By_BaseLine',$baseline)[Quantity &gt; 0]/UnitName]"/>


                <xsl:for-each select="$plannedUnits">
                  <xsl:sort select="UnitName" />
                  <TableCell>
                    <Paragraph>
                      <xsl:value-of select="UnitName" />
                    </Paragraph>
                  </TableCell>
                </xsl:for-each>
              </xsl:for-each>
            </TableRow>


            <xsl:variable name="plannedItems" select="/ProjectDataSet/Project/BacklogItem[sum(BacklogItemHours[BaseLine  = /ProjectDataSet/Project/Baseline and SprintUId = $currentPlanSprintUId]/Quantity)>0]"/>

            <!-- BACKLOG ITENS -->
            <xsl:for-each select="$plannedItems">
              <xsl:sort select="BusinessValue" data-type="number" order="descending" />
              <xsl:sort select="BacklogItemNumber" data-type="number" />
              <xsl:sort select="Title" />

              <xsl:variable name="backlogItemUId" select="BacklogItemUId" />

              <TableRow>
                <TableCell>
                  <Paragraph TextAlignment="Right">
                    <xsl:value-of select="BacklogItemNumber" />
                  </Paragraph>
                </TableCell>
                <TableCell>
                  <Paragraph>
                    <xsl:value-of select="Title" />
                  </Paragraph>
                </TableCell>
                <xsl:call-template name="StatusCell"/>
                <TableCell>
                  <Paragraph TextAlignment="Right">
                    <xsl:choose>
                      <xsl:when test="BusinessValue">
                        <xsl:value-of select="BusinessValue" />
                      </xsl:when>
                      <xsl:otherwise>0</xsl:otherwise>
                    </xsl:choose>
                  </Paragraph>
                </TableCell>
                <TableCell>
                  <Paragraph TextAlignment="Right">
                    <xsl:value-of select="Size" />
                  </Paragraph>
                </TableCell>
                <!-- FOR EACH BASELINE-->
                <xsl:for-each select="$plannedSprints">
                  <xsl:sort select="SprintNumber" data-type="number" order="descending" />

                  <xsl:variable name="baseline" select="SprintNumber"/>

                  <xsl:variable name="plannedUnits"
                                select="../BacklogItemUnit[UnitName = key('Hours_By_BaseLine',$baseline)[Quantity &gt; 0]/UnitName]"/>


                  <xsl:for-each select="$plannedUnits">
                    <xsl:sort select="UnitName" />

                    <xsl:variable name="unitName" select="UnitName" />

                    <xsl:variable name="hours" select="/ProjectDataSet/Project/BacklogItem/BacklogItemHours[BacklogItemUId=$backlogItemUId and UnitName=$unitName and BaseLine=$baseline]/Quantity"/>

                    <xsl:variable name ="hoursText">
                      <xsl:choose>
                        <xsl:when test ="$hours &gt; 0">
                          <xsl:value-of select="$hours"/>
                        </xsl:when>
                        <xsl:otherwise></xsl:otherwise>
                      </xsl:choose>
                    </xsl:variable>

                    <xsl:variable name="border">
                      <xsl:choose>
                        <xsl:when test="position() = last()">{StaticResource RightBorderCell}</xsl:when>
                        <xsl:otherwise>{StaticResource NormalCell}</xsl:otherwise>
                      </xsl:choose>
                    </xsl:variable>
                    <TableCell Style="{$border}">
                      <Paragraph TextAlignment="Right">
                        <xsl:value-of select="$hoursText"/>
                      </Paragraph>
                    </TableCell>

                    <!--<xsl:call-template name="AlternateColorTD">
                      <xsl:with-param name="number" select="$baseline"/>
                      <xsl:with-param name="align" select="'Right'"/>
                      <xsl:with-param name="text" select="$hoursText"/>
                    </xsl:call-template>-->

                  </xsl:for-each>
                </xsl:for-each>
              </TableRow>

            </xsl:for-each>

            <xsl:call-template name="PlanFooter">
              <xsl:with-param name="plannedSprints" select="$plannedSprints"/>
              <xsl:with-param name="plannedItems" select="$plannedItems"/>
              <xsl:with-param name="sprintNumber" select="$plannedSprints[1]/SprintNumber"/>
              <xsl:with-param name="expirationDateString" select="$plannedSprints[1]/ExpirationDateString"/>
            </xsl:call-template>



          </TableRowGroup>




        </Table>
      </xsl:if>



    </FlowDocument>


  </xsl:template>

</xsl:stylesheet>